home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 January / january_2001.iso / intercd / root / ^4Developers / VB / visbasdb / VBDB / VBDB Code / VB5 Code / Class 6 / Example6-6.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-09-18  |  8.0 KB  |  281 lines

  1. VERSION 5.00
  2. Begin VB.Form frmAuthors 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Authors"
  5.    ClientHeight    =   2625
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4680
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   2625
  13.    ScaleWidth      =   4680
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.Data datAuthors 
  16.       Caption         =   "Data1"
  17.       Connect         =   "Access"
  18.       DatabaseName    =   "C:\VBDB\Working\Biblio.mdb"
  19.       DefaultCursorType=   0  'DefaultCursor
  20.       DefaultType     =   2  'UseODBC
  21.       Exclusive       =   0   'False
  22.       Height          =   300
  23.       Left            =   2520
  24.       Options         =   0
  25.       ReadOnly        =   0   'False
  26.       RecordsetType   =   1  'Dynaset
  27.       RecordSource    =   "SELECT * FROM Authors ORDER BY Author"
  28.       Top             =   120
  29.       Visible         =   0   'False
  30.       Width           =   1980
  31.    End
  32.    Begin VB.CommandButton cmdDone 
  33.       Caption         =   "Do&ne"
  34.       Height          =   375
  35.       Left            =   3240
  36.       TabIndex        =   13
  37.       TabStop         =   0   'False
  38.       Top             =   2160
  39.       Width           =   1215
  40.    End
  41.    Begin VB.CommandButton cmdDelete 
  42.       Caption         =   "&Delete"
  43.       Height          =   375
  44.       Left            =   1680
  45.       TabIndex        =   12
  46.       TabStop         =   0   'False
  47.       Top             =   2160
  48.       Width           =   1215
  49.    End
  50.    Begin VB.CommandButton cmdEdit 
  51.       Caption         =   "&Edit"
  52.       Height          =   375
  53.       Left            =   120
  54.       TabIndex        =   11
  55.       TabStop         =   0   'False
  56.       Top             =   2160
  57.       Width           =   1215
  58.    End
  59.    Begin VB.CommandButton cmdCancel 
  60.       Caption         =   "&Cancel"
  61.       Height          =   375
  62.       Left            =   3240
  63.       TabIndex        =   10
  64.       TabStop         =   0   'False
  65.       Top             =   1680
  66.       Width           =   1215
  67.    End
  68.    Begin VB.CommandButton cmdSave 
  69.       Caption         =   "&Save"
  70.       Height          =   375
  71.       Left            =   1680
  72.       TabIndex        =   9
  73.       TabStop         =   0   'False
  74.       Top             =   1680
  75.       Width           =   1215
  76.    End
  77.    Begin VB.CommandButton cmdAddNew 
  78.       Caption         =   "&Add New"
  79.       Height          =   375
  80.       Left            =   120
  81.       TabIndex        =   8
  82.       TabStop         =   0   'False
  83.       Top             =   1680
  84.       Width           =   1215
  85.    End
  86.    Begin VB.CommandButton cmdNext 
  87.       Caption         =   "Next =>"
  88.       Height          =   255
  89.       Left            =   2520
  90.       TabIndex        =   7
  91.       TabStop         =   0   'False
  92.       Top             =   1320
  93.       Width           =   1215
  94.    End
  95.    Begin VB.CommandButton cmdPrevious 
  96.       Caption         =   "<= Previous"
  97.       Height          =   255
  98.       Left            =   1080
  99.       TabIndex        =   6
  100.       TabStop         =   0   'False
  101.       Top             =   1320
  102.       Width           =   1215
  103.    End
  104.    Begin VB.TextBox txtYearBorn 
  105.       DataField       =   "Year Born"
  106.       DataSource      =   "datAuthors"
  107.       Height          =   285
  108.       Left            =   1200
  109.       Locked          =   -1  'True
  110.       MaxLength       =   4
  111.       TabIndex        =   2
  112.       Text            =   "Text"
  113.       Top             =   840
  114.       Width           =   1215
  115.    End
  116.    Begin VB.TextBox txtAuthor 
  117.       DataField       =   "Author"
  118.       DataSource      =   "datAuthors"
  119.       Height          =   285
  120.       Left            =   1200
  121.       Locked          =   -1  'True
  122.       TabIndex        =   1
  123.       Text            =   "Text2"
  124.       Top             =   480
  125.       Width           =   3375
  126.    End
  127.    Begin VB.TextBox txtAuthorID 
  128.       DataField       =   "Au_ID"
  129.       DataSource      =   "datAuthors"
  130.       Height          =   285
  131.       Left            =   1200
  132.       Locked          =   -1  'True
  133.       TabIndex        =   3
  134.       TabStop         =   0   'False
  135.       Text            =   "Text1"
  136.       Top             =   120
  137.       Width           =   1215
  138.    End
  139.    Begin VB.Label Label3 
  140.       Caption         =   "Year Born"
  141.       Height          =   255
  142.       Left            =   120
  143.       TabIndex        =   5
  144.       Top             =   840
  145.       Width           =   1215
  146.    End
  147.    Begin VB.Label Label2 
  148.       Caption         =   "Author Name"
  149.       Height          =   255
  150.       Left            =   120
  151.       TabIndex        =   4
  152.       Top             =   480
  153.       Width           =   1215
  154.    End
  155.    Begin VB.Label Label1 
  156.       Caption         =   "Author ID"
  157.       Height          =   255
  158.       Left            =   120
  159.       TabIndex        =   0
  160.       Top             =   120
  161.       Width           =   1215
  162.    End
  163. Attribute VB_Name = "frmAuthors"
  164. Attribute VB_GlobalNameSpace = False
  165. Attribute VB_Creatable = False
  166. Attribute VB_PredeclaredId = True
  167. Attribute VB_Exposed = False
  168. Option Explicit
  169. Private Sub ValidateData(AllOK As Boolean)
  170. Dim Message As String
  171. Dim InputYear As Integer, CurrentYear As Integer
  172. AllOK = True
  173. Message = ""
  174. 'Check for name
  175. If Len(txtAuthor.Text) = 0 Then
  176.   Message = "You must enter an Author Name." + vbCrLf
  177.   txtAuthor.SetFocus
  178.   AllOK = False
  179. End If
  180. 'Check length and range on Year Born
  181. InputYear = Val(txtYearBorn.Text)
  182. CurrentYear = Val(Format(Now, "yyyy"))
  183. If Len(txtYearBorn.Text) <> 0 Then
  184.   If InputYear > CurrentYear Or InputYear < CurrentYear - 150 Then
  185.     Message = Message + "Year Born must be between" & Str(CurrentYear - 150) & " and" & Str(CurrentYear) & "."
  186.     txtYearBorn.SetFocus
  187.     AllOK = False
  188.   End If
  189. End If
  190. If Not (AllOK) Then
  191.   MsgBox Message, vbOKOnly + vbInformation, "Validation Error"
  192. End If
  193. End Sub
  194. Private Sub cmdAddNew_Click()
  195. Call SetState("Add")
  196. End Sub
  197. Private Sub cmdCancel_Click()
  198. Call SetState("View")
  199. End Sub
  200. Private Sub cmdDelete_Click()
  201. Dim Response As Integer
  202. Response = MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete")
  203. If Response = vbNo Then
  204.   Exit Sub
  205. End If
  206. End Sub
  207. Private Sub cmdEdit_Click()
  208. Call SetState("Edit")
  209. End Sub
  210. Private Sub cmdNext_Click()
  211. datAuthors.Recordset.MoveNext
  212. If datAuthors.Recordset.EOF Then
  213.   Beep
  214.   datAuthors.Recordset.MoveLast
  215. End If
  216. End Sub
  217. Private Sub cmdPrevious_Click()
  218. datAuthors.Recordset.MovePrevious
  219. If datAuthors.Recordset.BOF Then
  220.   Beep
  221.   datAuthors.Recordset.MoveFirst
  222. End If
  223. End Sub
  224. Private Sub cmdSave_Click()
  225. Dim Valid As Boolean
  226. Call ValidateData(Valid)
  227. If Not (Valid) Then Exit Sub
  228. MsgBox "Record saved.", vbOKOnly + vbInformation, "Save"
  229. Call SetState("View")
  230. End Sub
  231. Private Sub SetState(AppState As String)
  232. Select Case AppState
  233. Case "View"
  234.   txtAuthorID.BackColor = vbWhite
  235.   txtAuthor.Locked = True
  236.   txtYearBorn.Locked = True
  237.   cmdPrevious.Enabled = True
  238.   cmdNext.Enabled = True
  239.   cmdAddNew.Enabled = True
  240.   cmdSave.Enabled = False
  241.   cmdCancel.Enabled = False
  242.   cmdEdit.Enabled = True
  243.   cmdDelete.Enabled = True
  244.   cmdDone.Enabled = True
  245.   txtAuthor.SetFocus
  246. Case "Add", "Edit"
  247.   txtAuthorID.BackColor = vbRed
  248.   txtAuthor.Locked = False
  249.   txtYearBorn.Locked = False
  250.   cmdPrevious.Enabled = False
  251.   cmdNext.Enabled = False
  252.   cmdAddNew.Enabled = False
  253.   cmdSave.Enabled = True
  254.   cmdCancel.Enabled = True
  255.   cmdEdit.Enabled = False
  256.   cmdDelete.Enabled = False
  257.   cmdDone.Enabled = False
  258.   txtAuthor.SetFocus
  259. End Select
  260. End Sub
  261. Private Sub Form_Activate()
  262. Call SetState("View")
  263. End Sub
  264. Private Sub txtAuthor_KeyPress(KeyAscii As Integer)
  265. If KeyAscii = vbKeyReturn Then
  266.   txtYearBorn.SetFocus
  267.   Exit Sub
  268. End If
  269. End Sub
  270. Private Sub txtYearBorn_KeyPress(KeyAscii As Integer)
  271. If KeyAscii = vbKeyReturn Then
  272.   txtAuthor.SetFocus
  273.   Exit Sub
  274. End If
  275. If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or KeyAscii = vbKeyBack Then
  276.   Exit Sub
  277.   Beep
  278.   KeyAscii = 0
  279. End If
  280. End Sub
  281.